﻿using System;
using System.Web.UI.WebControls;
using Blazing.BusinessObjects.Tagging;
using Iesi.Collections.Generic;
using Blazing.WebFacade;
using System.Collections.Generic;

namespace Blazing.UI.Cms.Admin.Usercontrols
{
    public partial class ObjectTagCloud : System.Web.UI.UserControl
    {
        private bool _AllowEditTaxonomyTags = true;
        public bool AllowEditTaxonomyTags
        {
            get
            {
                return _AllowEditTaxonomyTags;
            }
            set
            {
                _AllowEditTaxonomyTags = value;
                PanelEditTags.Visible = value;
            }
        }

        private ISet<TaxonomyTag> viewStateItems
        {
            get
            {
                if (ViewState["viewStateItems"] == null)
                    return new HashedSet<TaxonomyTag>();
                return ViewState["viewStateItems"] as ISet<TaxonomyTag>;
            }
            set
            {
                ViewState["viewStateItems"] = value;
            }
        }

        public ISet<TaxonomyTag> TaxonomyTags
        {
            get
            {
                //UpdateRemovedItems();
                return viewStateItems;
            }
            set
            {
                ISet<TaxonomyTag> tagList = new HashedSet<TaxonomyTag>();

                if (value != null)
                {
                    foreach (TaxonomyTag tag in value)
                    {
                        tagList.Add(TaxonomyTag.Load(tag.ID));
                    }
                }

                viewStateItems = tagList;
                repeaterTags.DataSource = tagList;
                repeaterTags.DataBind();
            }
        }

        bool _allowAddingOfTags = true;
        public bool AllowAdding
        {
            get { return _allowAddingOfTags; }
            set { _allowAddingOfTags = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnAddTags_Click(object sender, EventArgs e)
        {
            Taxonomy taxonomy = SelectTags1.SelectedTaxonomy;
            List<string> tagList = SelectTags1.Tags;
            TaggingController controller = new TaggingController();
            List<TaxonomyTag> addTags = new List<TaxonomyTag>();
            foreach (TaxonomyTag t in TaxonomyTags)
            {
                addTags.Add(TaxonomyTag.Load(t.ID));
            }
            addTags.AddRange(controller.ProcessTagList(taxonomy.ID, null, tagList, AllowAdding));
            TaxonomyTags = new HashedSet<TaxonomyTag>(addTags);
            SelectTags1.Clear();
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            //PopEx.Cancel();
        }

        protected void btnRemoveTag_Click(object sender, EventArgs e)
        {
            TaggingController controller = new TaggingController();
            Button btn = sender as Button;
            if (btn != null)
            {
                int id = int.Parse(btn.CommandArgument);
                ISet<TaxonomyTag> savedItems = TaxonomyTags;
                foreach (TaxonomyTag t in savedItems)
                    t.Refresh(); //load back unserialised properties

                TaxonomyTags = (ISet<TaxonomyTag>) controller.RemoveTagFromList(id, savedItems);
            }
        }
    }
}